package com.tarena.dubbo.server;

import com.tarena.dubbo.server.service.StockService;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        //使用socket监听端口 等待外界socket访问
        //每当访问一次,就帮助调用一次reduceStock
        ServerSocket server=new ServerSocket(20000);
        //每个socket都来处理,调用reduceStock
        while(true){
            //接收一个socket
            Socket socket = server.accept();
            System.out.println("服务端接收到了socket");
            //开始调用
            process(socket);
        }
    }

    private static void process(Socket socket) throws IOException {
        //从socket中拿到发送的流数据
        InputStream inputStream = socket.getInputStream();
        //携带这 到底要减库存 减几个 流反序列化 得到String
        DataInputStream dis=new DataInputStream(inputStream);
        //反序列化 String参数
        String num = dis.readUTF();
        StockService stockService=new StockService();
        stockService.reduceStock(num);
        //没有返回值,没有必要写outPutStream
    }
}
